package com.geostar.constructionland.index.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.nfunk.jep.JEP;
import org.nfunk.jep.Node;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yelll
 * @since 2020-4-4 17:15
 **/
public class ComplexExpUtil {
    private static String mathOnlyKey = "geostarkey";
    /**获取新值**/
    public static Map getTransedValue(String element, Double value, List<Map<String,String>> composeList) throws Exception{
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("orgvalue",value);
        resultMap.put("element",element);
        if(composeList!=null && composeList.size()>0){
            for(Map<String,String> composeMap : composeList){
                String e = composeMap.get("e");
                String r = composeMap.get("r").trim();
                boolean isOk = false;
                /**对r进行判断**/
                String[] requires = r.split("<");
                /*** 10 <= ele <= 10**/
                if(requires.length==3){
                    if(!element.equals(requires[1].replaceAll("=",""))){
                        continue;
                    }

                    Double frontValue = Double.valueOf(requires[0]);
                    Double behindValue = Double.valueOf(requires[2].replaceAll("=",""));
                    if(requires[1].startsWith("=")){
                        /**大于等于前面  例如 10 <= ele **/
                        if(frontValue<=value){
                            isOk = true;
                        }
                    }else{
                        /**大于前面 例如 10<ele**/
                        if(frontValue<value){
                            isOk = true;
                        }
                    }
                    /***判断是否大于***/
                    if(!isOk){
                        continue;
                    }
                    /**置为false进行二段判断**/
                    isOk = false;
                    if(requires[2].startsWith("=")){
                        /**小于等于后值**/
                        if(behindValue>=value){
                            isOk = true;
                        }
                    }else{
                        if(behindValue>value){
                            isOk = true;
                        }
                    }
                    if(!isOk){
                        continue;
                    }else{
                        /**计算值**/
                        JEP jep = new JEP();
                        /**UUID无法计算**/
//                        jep.addVariable(element,value);
                        jep.addVariable(mathOnlyKey,value);
                        Node node = jep.parse(e.replaceAll(element,mathOnlyKey));
                        Object evaluate = jep.evaluate(node);
                        resultMap.put("transedvalue",evaluate);
                    }
                }else if(requires.length==2){
                    Double behindValue = Double.valueOf(requires[1].replaceAll("=",""));
                    if(requires[1].startsWith("=")){
                        /**小于等于后值**/
                        if(behindValue>=value){
                            isOk = true;
                        }
                    }else{
                        if(behindValue>value){
                            isOk = true;
                        }
                    }
                    if(!isOk){
                        continue;
                    }else{
                        /**计算值**/
                        JEP jep = new JEP();
                        jep.addVariable(mathOnlyKey,value);
                        Node node = jep.parse(e.replaceAll(element,mathOnlyKey));
                        Object evaluate = jep.evaluate(node);
                        resultMap.put("transedvalue",evaluate);
                    }
                }else{
                    requires = r.split(">");
                    Double fontValue = Double.valueOf(requires[1].replaceAll("=",""));
                    if(requires[1].startsWith("=")){
                        /**小于等于后值**/
                        if(fontValue<=value){
                            isOk = true;
                        }
                    }else{
                        if(fontValue<value){
                            isOk = true;
                        }
                    }
                    if(!isOk){
                        continue;
                    }else{
                        /**计算值**/
                        JEP jep = new JEP();
                        jep.addVariable(mathOnlyKey,value);
                        Node node = jep.parse(e.replaceAll(element,mathOnlyKey));
                        Object evaluate = jep.evaluate(node);
                        resultMap.put("transedvalue",evaluate);
                    }
                }
            }
        }
        return resultMap;
    }

    public static List<Map<String,String>> getComposeList(String express,String element) {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> expMap = null;
        try {
            expMap = mapper.readValue(express, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Map<String, String>> composeList = null;
        List<Map<String,Object>> elesList = (List)expMap.get("eles");
        for(Map<String,Object> eleMap:elesList) {
            String ele = eleMap.get("orgele").toString();
            if(!ele.equals(element)){
                continue;
            }
            composeList = (List) eleMap.get("compose");
        }
        return composeList;
    }

    public static String getTransedExpress(String express) {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> expMap = null;
        try {
            expMap = mapper.readValue(express, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return (String)expMap.get("express");
    }

    public static String getTransedEle(String express,String element) {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> expMap = null;
        try {
            expMap = mapper.readValue(express, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String transedEle = null;
        List<Map<String,Object>> elesList = (List)expMap.get("eles");
        for(Map<String,Object> eleMap:elesList) {
            String ele = eleMap.get("orgele").toString();
            if(!ele.equals(element)){
                continue;
            }
            transedEle = (String) eleMap.get("ele");
        }
        return transedEle;
    }
}
